Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
## Chapter 12 Graphics Visual Basic is a graphics programmer’s paradise. Many difficult graphics tasks are made easy with Visual Basic’s controls and methods. Working with graphics in Visual Basic involves only a few objects: the Picture Box, Image, PictureClip, Shape, and Line controls, and the Picture, Printer, and Form objects. This may seem like a small cast of characters, but you’ll soon see they provide a rich and powerful set of graphics capabilities. ### Twiddling Your Twips—The Visual Basic Coordinate System Before we get to the meat and potatoes of this chapter, you need to understand Visual Basic’s coordinate system. Coordinates are used to define the position of any object displayed on the screen. For some objects, the coordinate system is also used to define the object’s size (its height and width). An object’s position is always expressed as the distance of its top-left corner from the top-left corner of its container object. A form’s container is always the Screen object, which I’ll get to in a moment. The one exception to this rule is MDI (Multiple Document Interface) child forms, whose container is the MDI parent form. A control’s container is always the form it is on, unless the control is placed in a Picture Box or Frame, in which case the Picture Box or Frame is the container. The only other possible container is the Printer object, which is used for (you guessed it) printing. I’ll deal with the Printer object later in the chapter. Visual Basic’s coordinate system works like the Cartesian coordinate graphing system you learned in school. Any point is represented by two numbers. One of these numbers (traditionally called X) indicates the point’s horizontal position, while the other number (Y) indicates the point’s vertical position. Of course, a coordinate system must have a zero point, or origin—the point where both X and Y are 0. It also must have a scale that relates the coordinate units to real measurement units. Does an X value of two mean two inches, two yards, or two furlongs? In all Visual Basic coordinate systems, the default origin is located at the top-left corner of the container. Positive X values move to the right, and positive Y values move down. Negative coordinates are possible, representing positions above or to the left of the default origin. Note that I said default origin, implying that the origin can be positioned elsewhere. This is indeed true—at least for some container objects—but let’s not worry about that for now. You can use several different scales in Visual Basic coordinates. While many Visual Basic programs are written using only the default scale, you need to know the available options—particularly if you will be doing a lot of graphics programming. The available scale settings are shown in Table 12.1. Are you wondering what logical means? A logical unit will print at the proper size. For example, a line that is one logical inch long will measure one inch when printed. You set a container object’s scale by using the ScaleMode property; however, the only objects that have a ScaleMode property are Form, Picture Box, and Printer. The other objects that can be containers—the Screen object and the Frame control—always use twips for measurement. Table 12.1 Visual Basic scale settings.
ScaleMode Value | Scale Units
--- | ---
0 | Custom
1. | Twip (default); there are 1,440 twips per logical inch and 567 twips per logical centimeter
2. | Point; there are 72 points per logical inch
3. | Pixel; this is the smallest unit of monitor or printer resolution
4. | Character; one character unit equals 120 twips horizontally and 240 twips vertically
5. | Inch
6. | Millimeter
7 | Centimeter
Before we look at the various object properties that are related to the coordinate system, let’s look at the Big Daddy (or Big Mommy, if you prefer) of all containers, the Screen object. #### The Screen Object The Screen object is Visual Basic’s logical representation of the entire display screen. Screen coordinates are always measured in twips, and the top-left corner is always located at (0, 0). The physical size of the screen varies from system to system; knowing the screen size will help you make the best use of available screen real estate. You wouldn’t want your program’s forms to extend off the edge of the screen. You can query two of the Screen object’s properties, Width and Height, to obtain the screen size:
    ScreenWidthInTwips = Screen.Width
    ScreenHeightInTwips = Screen.Height
Although you can’t change these properties, knowing their values—and hence, the screen size—will help you determine the appropriate position and size for your forms. The Screen object has some other properties you need to know about. The TwipsPerPixelX and TwipsPerPixelY properties return the number of twips per screen pixel. A pixel is the smallest dot of light that can be displayed on the screen, and the physical resolution of a particular display is expressed in terms of horizontal and vertical pixels. When a program is running, the number of twips per pixel will depend on the system’s hardware configuration, as well as the settings of the Windows display driver. You can use these properties to match your program’s graphics to the screen characteristics. For example, to draw the thinnest possible horizontal line, you would set the line thickness equal to Screen.TwipsPerPixelY twips. The result is a line that is precisely one pixel thick. You can also calculate the current screen resolution—the number of pixels horizontally and vertically:
    Xres = Screen.Width / Screen.TwipsPerPixelX
    Yres = Screen.Height / Screen.TwipsPerPixelY
The MousePointer property specifies the appearance of the mouse pointer while it is over a Visual Basic screen element. With the default setting of 0, the pointer is controlled by the MousePointer property of the individual objects in the program (form, control, etc.)—that is, whichever object the mouse happens to be over at the moment. Other possible settings for the Screen object’s MousePointer property are given in Table 12.2. A MousePointer property setting of 99 lets you define your own cursor using the MouseIcon property. In the statement
    Screen.MouseIcon = picture
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.